package plugin.menumarble;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import plugin.menumarble.transformations.NAME_FOLDER;
import business.level0.Level0;

public class Agente {

	public static void LoadFile(String pathSourceFile, String pathDestinationFolder, String nameProject) throws IOException, CoreException{
		AddFileToFolder(pathSourceFile,pathDestinationFolder);
		/*Actualizar la carpeta L0*/
		RefreshFolderL0(nameProject);
	}
	
	/*LoadFolder(que carpeta cargar, en que carpeta lo cargo, nombre del proyecto)*/
	public static void LoadFolder(String pathSourceFolder, String pathDestinationFolder, String nameProject) throws IOException, CoreException {
		// TODO Auto-generated method stub
		
		File folder=new File(pathSourceFolder); //carpeta que queremos cargar
		/*Vemos si tiene archivos .java o carpetas dentro*/
		ArrayList<File> files= Level0.getListFiles(pathSourceFolder,"java");
		
		if(!files.isEmpty()){
			String newPathFolder=pathDestinationFolder+"/"+folder.getName();
			File newFolder=new File(newPathFolder);
			newFolder.mkdir();
			for(File f:files){
				if(f.isFile()){
					AddFileToFolder(f.getPath(),newPathFolder);
				}
				if(f.isDirectory()){
					LoadFolder(f.getPath(),newPathFolder, nameProject);
				}
			}
		}
		
		/*Actualizar la carpeta L0*/
		RefreshFolderL0(nameProject);

	}
	
	private static void AddFileToFolder(String pathFile, String pathFolder) throws IOException{
		File file=new File(pathFile);
		String pathNewFile=pathFolder+"/"+file.getName();
		/*Se crea un archivo vacio en esa ruta*/
		File newFile = new File (pathNewFile);
		newFile.createNewFile();
		/*Se hace una copia del archivo a ese archivo vacio nuevo*/
		FileCopy(file.getPath(),newFile.getPath());
		
		
	}
	
	private static void FileCopy(String path_sourceFile, String path_destinationFile) {
 
		try {
			File inFile = new File(path_sourceFile);
			File outFile = new File(path_destinationFile);
 
			FileInputStream in = new FileInputStream(inFile);
			FileOutputStream out = new FileOutputStream(outFile);

			
			int c;
			while( (c = in.read() ) != -1){
				out.write(c);
			}
			in.close();
			out.close();
		} catch(IOException e) {
			System.err.println("Hubo un error de entrada/salida!!!");
		}
	}
	
	public static void OpenFileInWorkspace(File fileToOpen) throws PartInitException{
		if (fileToOpen.exists() && fileToOpen.isFile()) {
		    IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
		    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		 
		    try {
		        IDE.openEditorOnFileStore( page, fileStore );
		    } catch ( PartInitException e ) {
		        //Put your exception handler here if you wish to
		    }
		} else {
		    //Do something if the file does not exist
		}
		
		
	}

	public static String getPathFolder(String nameFolder, String nameProject)throws IllegalArgumentException {
				
		IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(nameProject);
		//System.out.println("** La ruta del proyecto es: "+newProject.getLocation().toString());
		IFolder folder = newProject.getFolder(nameFolder);
		String pathFolder = folder.getLocation().toString();
		//System.out.println("** La ruta de la carpeta "+nameFolder+" es: "+pathFolder);
		return pathFolder;
	}
	

	private static void RefreshFolder(String nameFolder, String nameProject) throws CoreException {
		IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(nameProject);
		IFolder folder = newProject.getFolder(nameFolder);
		/*Actualizar carpeta*/
		folder.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
	}
	
	public static void RefreshFolderL0(String nameProject) throws CoreException {
		// TODO Auto-generated method stub
		RefreshFolder(NAME_FOLDER.L0, nameProject);
		
	}
	
	public static void RefreshFolderL1(String nameProject) throws CoreException {
		// TODO Auto-generated method stub
		RefreshFolder(NAME_FOLDER.L1, nameProject);
		
	}
	public static void RefreshFolderL2(String nameProject) throws CoreException {
		// TODO Auto-generated method stub
		RefreshFolder(NAME_FOLDER.L2, nameProject);
		
	}
	public static void RefreshFolderL3(String nameProject) throws CoreException {
		// TODO Auto-generated method stub
		RefreshFolder(NAME_FOLDER.L3, nameProject);
		
	}



	
	
}
